/*
 * Copyright 2015 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.inferred.freebuilder.client.rpc;

import com.google.gwt.core.client.GWT;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;

public class StringListTest extends GWTTestCase {

    private static final int RPC_TIMEOUT = 15000;
    private static final String MODULE_NAME = "org.inferred.freebuilder.TestServer";
    private static final StringListGwtType VALUE = new StringListGwtType.Builder()
            .addNames("john", "alice", "sam")
            .build();

    private StringListTestServiceAsync customFieldSerializerTestService;

    @Override
    public String getModuleName() {
        return MODULE_NAME;
    }

    @Override
    protected void gwtSetUp() throws Exception {
        super.gwtSetUp();
        delayTestFinish(RPC_TIMEOUT);
    }

    public void testSerialization() {
        StringListTestServiceAsync service = getServiceAsync();
        service.echo(VALUE, new AsyncCallback<StringListGwtType>() {
            @Override
            public void onFailure(Throwable caught) {
                throw new AssertionError(caught);
            }

            @Override
            public void onSuccess(StringListGwtType result) {
                assertEquals(VALUE, result);
                finishTest();
            }
        });
    }

    private StringListTestServiceAsync getServiceAsync() {
        if (customFieldSerializerTestService == null) {
            customFieldSerializerTestService =
                    (StringListTestServiceAsync) GWT.create(StringListTestService.class);
            ((ServiceDefTarget) customFieldSerializerTestService)
                    .setServiceEntryPoint(GWT.getModuleBaseURL() + "string-list");
        }
        return customFieldSerializerTestService;
    }
}
